package com.acc.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.AdminRole;
import com.acc.entity.Msg;
import com.acc.entity.RoleName;
import com.acc.service.AdminUserService;
import com.acc.service.AuthorityService;
import com.acc.service.RoleService;
import com.acc.service.impl.AdminUserServiceImpl;
import com.acc.service.impl.AuthorityServiceImpl;
import com.acc.service.impl.RoleServiceImpl;
import com.alibaba.fastjson.JSON;

public class RoleController extends InnerDispacherController2 {

	/**
	 * @author 朱春阳
	 */
	private static final long serialVersionUID = 1L;

	public void deleteRole(HttpServletRequest request, HttpServletResponse response) {
		// 从页面获取id
		String roleId = request.getParameter("roleId");
		Integer role_id = null;
		try {
			role_id = Integer.parseInt(roleId);
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		RoleService roleService = new RoleServiceImpl();

		// 判断删除是否成功
		boolean flag = roleService.deleteOneRole(role_id);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("删除失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		response.setContentType("application/json");
		try {
			response.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 增加一个角色
	 * @param req
	 * @param resp
	 */
	public void addRole(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("??????????????????????????????????/");
		RoleService roleService = new RoleServiceImpl();
		// 从输入框中取出值
		String name = req.getParameter("roleName");
		System.out.println(name);
		// 将取出的值封装起来
		RoleName rolename = new RoleName();
		rolename.setRoleName(name);

		// 增加记录
		boolean flag = roleService.addOneRole(rolename);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("增加失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		try {
			resp.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
